"""Property model for Real Estate CRM system."""

from __future__ import annotations

from typing import TYPE_CHECKING

from datetime import datetime
from sqlalchemy import (
    Boolean,
    DateTime,
    ForeignKey,
    Integer,
    Numeric,
    String,
    Text,
    Enum as SQLEnum,
    Date,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship

from ..db.base_class import Base
from ..models.base import TimestampMixin
from ..models.enums import PropertyStatus, PropertyType

if TYPE_CHECKING:
    from ..models.property_photo import PropertyPhoto
    from ..models.visitor_sheet import VisitorSheet


class Property(Base, TimestampMixin):
    """Property entity for Real Estate CRM."""

    __tablename__ = "properties"

    id: Mapped[int] = mapped_column(primary_key=True, index=True)

    # Basic Information
    community_name: Mapped[str] = mapped_column(
        String(255), nullable=False, index=True, comment="小区名称"
    )
    address: Mapped[str] = mapped_column(String(500), nullable=False, comment="详细地址")
    room_number: Mapped[str] = mapped_column(String(50), nullable=False, comment="房号")

    # Property Details
    layout: Mapped[str] = mapped_column(String(50), nullable=False, comment="户型 (如: 2室1厅)")
    area: Mapped[float | None] = mapped_column(
        Numeric(10, 2), nullable=True, comment="面积(平方米)"
    )
    floor_info: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="楼层信息")
    orientation: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="朝向")
    decoration: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="装修情况")

    # Pricing
    rent_price: Mapped[int] = mapped_column(Integer, nullable=False, comment="租金(元/月)")

    # Landlord Info
    landlord_name: Mapped[str | None] = mapped_column(
        String(100), nullable=True, comment="房东姓名"
    )
    landlord_phone: Mapped[str] = mapped_column(String(20), nullable=False, comment="房东电话")

    # Status
    status: Mapped[PropertyStatus] = mapped_column(
        SQLEnum(PropertyStatus),
        nullable=False,
        default=PropertyStatus.VACANT,
        index=True,
        comment="房源状态",
    )
    property_type: Mapped[PropertyType] = mapped_column(
        SQLEnum(PropertyType), nullable=False, default=PropertyType.APARTMENT, comment="房源类型"
    )

    # Availability
    available_date: Mapped[Date | None] = mapped_column(Date, nullable=True, comment="可租日期")

    # Additional Info
    notes: Mapped[str | None] = mapped_column(Text, nullable=True, comment="备注(配套设施等)")

    # Soft delete fields
    is_deleted: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=False, index=True, comment="软删除标记"
    )
    deleted_at: Mapped[datetime | None] = mapped_column(
        DateTime, nullable=True, comment="软删除时间戳"
    )
    deleted_by: Mapped[int | None] = mapped_column(
        Integer, nullable=True, comment="执行删除的用户ID"
    )

    # Relationships
    photos: Mapped[list[PropertyPhoto]] = relationship(
        "PropertyPhoto", back_populates="property", cascade="all, delete-orphan"
    )
    visitor_sheets: Mapped[list[VisitorSheet]] = relationship(
        "VisitorSheet", back_populates="property"
    )
